package com.vodone.v1pin.detail.widgets
{
	import com.vodone.v1pin.productlist.vo.QueryItemVO;
	
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	
	public class ColorRenderer extends Canvas
	{
		public function ColorRenderer()
		{
			super();
			this.width = 25;
			this.height = 25;
			this.setStyle("borderThickness",1);
			this.setStyle("borderStyle" ,  "solid");
			this.buttonMode = true;
			addEventListener( MouseEvent.MOUSE_OVER  , mouseOverHandler );
			addEventListener( MouseEvent.MOUSE_OUT  , mouseOutHandler );
		}
		
		private function mouseOutHandler( e:MouseEvent ):void
		{
			if( !selected )
			{
				setWhite();
			}
		}
		private function mouseOverHandler( e:MouseEvent ):void
		{
			if(!selected)
			{
				setBlack();
			}
		}
		private var selectedChanged:Boolean = true;
		private var _selected:Boolean = false;
		public function get selected():Boolean
		{
			return _selected;
		}
		public function set selected(value:Boolean):void
		{
			_selected = value;
			buttonMode = !value;
			if( !selected )
			{
				setWhite();
			}
			else
			{
				setBlack();
			}
		}
		private function setWhite():void
		{
			setStyle("borderColor",0xcccccc);
		}
		
		private function setBlack():void
		{
			setStyle("borderColor",0x000000);
		}
		
		private function getColor(item:Object):int
		{
			var color:int = int( item["extend"] );
			return color;
		}
		
		private var dataDirty:Boolean = false;
		private var _item:QueryItemVO;
		public function get item():QueryItemVO
		{
			return _item;
		}
		
		private var isColor:Boolean = true;
		public function set item(value:QueryItemVO):void
		{
			_item = value;
			dataDirty = true;
			isColor = item.extend.indexOf("http") < 0;
			invalidateProperties();
		}
		
		protected override function commitProperties():void
		{
			super.commitProperties();
			if( dataDirty )
			{
				if( isColor )
				{
					this.setStyle("backgroundColor",getColor( item ) );
				}
				else
				{
					checkImage();
					img.source = item["extend"];
				}
			}
		}
		
		private var img:Image;
		private function checkImage():void
		{
			if( img == null )
			{
				img = new Image();
				img.width = this.width;
				img.height = this.height;
				addChild( img );
			}
		}
	}
}